package com.flu.holidays.service.impl.db;


import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.flu.holidays.constant.DayEnum;
import com.flu.holidays.modeel.DayTypeModel;
import com.flu.holidays.service.HolidayService;
import com.flu.holidays.service.impl.db.mapper.HolidayMapper;
import com.flu.holidays.service.impl.db.entity.HolidayEntity;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Slf4j
@RequiredArgsConstructor
public class HolidayServiceDbImpl extends ServiceImpl<HolidayMapper, HolidayEntity> implements HolidayService {




    @Override
    public DayTypeModel getDayType() {
        return getDayType(DateUtil.today());
    }

    @Override
    public DayTypeModel getDayType(String date) {


        DateTime dateTime = DateUtil.parse(date, DatePattern.NORM_DATE_FORMATTER);

        LambdaQueryWrapper<HolidayEntity> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(HolidayEntity::getYear, dateTime.year());
        //没有对应年份的假日配置
        if (!this.baseMapper.exists(queryWrapper)) {
            return getDefaultModel(dateTime);
        }
        String formatDt = DateUtil.format(dateTime, DatePattern.PURE_DATE_FORMATTER);

        queryWrapper.eq(HolidayEntity::getDt,formatDt);
        List<HolidayEntity> holidays = this.baseMapper.selectList(queryWrapper);
        //指定日期不是节假日/调休
        if (CollectionUtil.isEmpty( holidays)){
            if (DateUtil.isWeekend(dateTime)){
                return DayEnum.WEEKEND.toModel();
            }
            return DayEnum.WORKING_DAY.toModel();
        }
        if (holidays.size()>1){
            throw new RuntimeException(date+"日出现重复的配置");
        }
        //指定日期是节假日/调休
        HolidayEntity holidayEntity = holidays.get(0);
        Optional<DayEnum> optional = DayEnum.getByCodeInt(holidayEntity.getType());
        if (optional.isPresent()){
            DayTypeModel model = optional.get().toModel();
            model.setType(holidayEntity.getHolidayType());
            return model;
        }else {
            throw new RuntimeException(date+"日配置错误");
        }

    }

    public JSONObject getJsonConfig(){
        LambdaQueryWrapper<HolidayEntity> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.orderByAsc(HolidayEntity::getDt);
        List<HolidayEntity> holidayEntities = this.baseMapper.selectList(queryWrapper);
        if (CollectionUtil.isEmpty(holidayEntities)){
            return null;
        }
        List<Integer> years = holidayEntities.stream().map(HolidayEntity::getYear).distinct().collect(Collectors.toList());
        //根对象
        JSONObject rootObject = new JSONObject();
        for (Integer year : years) {

            List<HolidayEntity> yearHolidays = holidayEntities.stream().filter(holidayEntity -> holidayEntity.getYear().equals(year)).collect(Collectors.toList());
            //年份对象
            JSONObject holidayJson = new JSONObject();
            for (HolidayEntity yearHoliday : yearHolidays) {
                //日期对象
                entityToJson(yearHoliday,holidayJson);
            }
            rootObject.set(year+"",holidayJson);

        }

        return rootObject;
    }


    /**
     * entity转json
     * @param entity
     * @param parentJson
     */
    private void entityToJson(HolidayEntity entity, JSONObject parentJson) {
        JSONObject json = new JSONObject();
        json.set("type", entity.getType());
        json.set("holidayType", entity.getHolidayType());
        String day = DateUtil.format(DateUtil.parse(entity.getDt() + "", DatePattern.PURE_DATE_FORMATTER), DatePattern.NORM_DATE_FORMATTER);
        parentJson.set(day, json);
    }


}
